using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Curriculum.Business.Manejadores;
using Curriculum.Business.Clases;
using Curriculum.Business.ClasesPersistentes;

namespace Curriculum.UI
{
	/// <summary>
	/// Summary description for frmConvertirAntecedente.
	/// </summary>
	public class frmReclasificarAntecedente : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnAceptar;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnCancelar;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TreeView treeViewEstructura;
		private System.Windows.Forms.Label label1;
		private Form _instanceRef;
		private string _idAntecedentePersistente;
		private string _idAntecenteRaizOri;
		private string _idAntecenteHojaOri;
		// idAntecedenteRaiz del arbol
		private string _idAntecedenteRaizDes;
		// idAntecedenteHoja del arbol
		private string _idAntecedenteHojaDes;
		private VersionCurriculo _versionCV;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmReclasificarAntecedente()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void LlenarEstructura()
		{
			// obtengo la estructura con el manejador de Antecedentes
			ManejadorAntecedentes ma = ManejadorAntecedentes.GetInstancia();
			// cargo el treeview de la estructra
			CargarArbol(ma.Estructura);
			treeViewEstructura.Nodes[0].Expand();
		}
		/// <summary>
		/// Carga el arbol de estructura
		/// </summary>
		/// <param name="raiz"></param>
		private void CargarArbol(Antecedente raiz)
		{
			Helper.TreeViewHelper.CargarArbol(treeViewEstructura,  raiz);			
		}		

		/// <summary>
		/// Muestra un mensaje
		/// </summary>
		/// <param name="msj">mensaje a mostrar</param>
		/// <param name="mbi">tipo de icono que se muestra</param>
		private void MostrarMensaje(string msj, MessageBoxIcon mbi)
		{
			MessageBox.Show(msj,
				"Reclasificacion",
				MessageBoxButtons.OK,
				mbi);			
		}
		private void MostrarMensajeExito()
		{
			MostrarMensaje("El antecedente se ha reclasificado correctamente",MessageBoxIcon.Information);
		}
		private void MostrarMensajeFracaso()
		{
			MostrarMensaje("El antecedente al que se quiere reclasificar tiene una estructura diferente",MessageBoxIcon.Information);
		}
		
		private string GetIDAntecedenteRaiz(MyTreeNode tNode)
		{
			string id = "0";
			while(tNode.Id != "0")
			{
				id = tNode.Id;
				tNode = (MyTreeNode)tNode.Parent;
			}			
			return id;
		}
		
		/// <summary>
		/// Obtiene el antecedente Raiz y hoja
		/// </summary>
		private bool GetIdsAntecedente()
		{
			MyTreeNode nodo = (MyTreeNode) treeViewEstructura.SelectedNode;
			if(nodo!=null)
			{
				if(nodo.FirstNode == null)
				{
					_idAntecedenteHojaDes = nodo.Id;
					_idAntecedenteRaizDes = GetIDAntecedenteRaiz(nodo);				
				}
				else
				{
					MostrarMensaje("Debe Seleccionar un nodo hoja",MessageBoxIcon.Exclamation);
					return false;
				}								
			}
			else
			{
				MostrarMensaje("Debe Seleccionar un nodo hoja",MessageBoxIcon.Exclamation);				
				return false;
			}

			return true;
		}

		#region - Properties -

		public Form InstanceRef
		{
			get { return _instanceRef; }
			set { _instanceRef = value; }
		}

		public string IdAntecedentePersistente
		{
			get { return _idAntecedentePersistente; }
			set { _idAntecedentePersistente = value; }
		}

		public string IdAntecenteRaizOri
		{
			get { return _idAntecenteRaizOri; }
			set { _idAntecenteRaizOri = value; }
		}

		public string IdAntecenteHojaOri
		{
			get { return _idAntecenteHojaOri; }
			set { _idAntecenteHojaOri = value; }
		}

		public VersionCurriculo VersionCV
		{
			get { return _versionCV; }
			set { _versionCV = value; }
		}

		#endregion - Properties -

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmReclasificarAntecedente));
			this.btnAceptar = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnCancelar = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label1 = new System.Windows.Forms.Label();
			this.treeViewEstructura = new System.Windows.Forms.TreeView();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnAceptar
			// 
			this.btnAceptar.Location = new System.Drawing.Point(16, 16);
			this.btnAceptar.Name = "btnAceptar";
			this.btnAceptar.TabIndex = 2;
			this.btnAceptar.Text = "&Aceptar";
			this.btnAceptar.Click += new System.EventHandler(this.btnAceptar_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.btnCancelar);
			this.groupBox1.Controls.Add(this.btnAceptar);
			this.groupBox1.Location = new System.Drawing.Point(1, 258);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(408, 49);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			// 
			// btnCancelar
			// 
			this.btnCancelar.Location = new System.Drawing.Point(314, 16);
			this.btnCancelar.Name = "btnCancelar";
			this.btnCancelar.TabIndex = 3;
			this.btnCancelar.Text = "&Cancelar";
			this.btnCancelar.Click += new System.EventHandler(this.btnCancelar_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.label1);
			this.groupBox2.Controls.Add(this.treeViewEstructura);
			this.groupBox2.Location = new System.Drawing.Point(0, -2);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(408, 260);
			this.groupBox2.TabIndex = 4;
			this.groupBox2.TabStop = false;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(272, 23);
			this.label1.TabIndex = 3;
			this.label1.Text = "Seleccione el antecedente al que se quiere cambiar. ";
			// 
			// treeViewEstructura
			// 
			this.treeViewEstructura.ImageIndex = -1;
			this.treeViewEstructura.Location = new System.Drawing.Point(8, 40);
			this.treeViewEstructura.Name = "treeViewEstructura";
			this.treeViewEstructura.SelectedImageIndex = -1;
			this.treeViewEstructura.Size = new System.Drawing.Size(392, 208);
			this.treeViewEstructura.TabIndex = 2;
			// 
			// frmReclasificarAntecedente
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 309);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "frmReclasificarAntecedente";
			this.Text = "Recladificar Antecedente";
			this.Load += new System.EventHandler(this.frmReclasificarAntecedente_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmReclasificarAntecedente_Load(object sender, System.EventArgs e)
		{
			LlenarEstructura();
		}

		private void btnCancelar_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnAceptar_Click(object sender, System.EventArgs e)
		{
			if (GetIdsAntecedente())
			{
				ParIdRaizHoja origen;
				origen.idAntecedenteRaiz = IdAntecenteRaizOri;
				origen.idAntecedenteHoja = IdAntecenteHojaOri;
				ParIdRaizHoja destino;
				destino.idAntecedenteRaiz = _idAntecedenteRaizDes;
				destino.idAntecedenteHoja = _idAntecedenteHojaDes;
				
				if (_versionCV.Mover(_idAntecedentePersistente,origen, destino))
				{
					MostrarMensajeExito();
					((Curriculum)((AntecedenteABM)InstanceRef).InstanceRef).RefrescarArbolAntecedentes();
					InstanceRef.Close();
					this.Close();
				}
				else
				{
					MostrarMensajeFracaso();
				}

			}
		}

	}
}
